//
//  JJRecordCell.m
//  Estate
//
//  Created by houzhen on 2017/11/10.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "JJRecordCell.h"

@implementation JJRecordCell

-(void)setContentType:(contentStyle)contentType{
    _contentType = contentType;
    switch (contentType) {
        case DefaultMode:
            self.content.keyboardType = UIKeyboardTypeNumbersAndPunctuation ;
            break;
        case ZifuMode:
            self.content.keyboardType = UIKeyboardTypeDefault;
            break;
        case numMode:
            self.content.keyboardType = UIKeyboardTypeDecimalPad;
            break;
        case NumAndZifuMode:
            self.content.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            break;
        default:
            break;
    }
}
- (void)setIsEdit:(BOOL)isEdit{
    if(isEdit){
        self.content.delegate=self;
    }else{
        self.content.userInteractionEnabled=NO;
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    self.titleLabel.textColor=UIColorFromRGB(0x797979);
    self.titleLabel.font = [UIFont systemFontOfSize:16*BIZ];
    self.unitLabel.font = [UIFont systemFontOfSize:16*BIZ];
    self.unitLabel.textColor = UIColorFromRGB(0x797979);
    self.unitLabel.text = @"";
    self.content.keyboardType = UIKeyboardTypeDecimalPad ;
}

- (IBAction)contentChange:(ESTextField *)sender {
    if(self.contentChangeBlock){
        self.contentChangeBlock(self.content.text);
    }
}
#pragma mark UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.content resignFirstResponder];

    return  YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
   
    //   限制苹果系统输入法  禁止输入表情
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    
    if (self.contentType == ZifuMode) {
      
        return YES;
    }
    
    if (self.contentType == numMode) {
        if ([string length] > 0) {
            unichar single = [string characterAtIndex:0];//当前输入的字符
            if (single >= '0' && single <= '9'){
                return YES;
            }else{
                [self showError:@"只能输入数字"];
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;
            }
        }
    }
    if (self.contentType == NumAndZifuMode) {
        if ([string length] > 0) {
            unichar single = [string characterAtIndex:0];//当前输入的字符
            if ((single >= '0' && single <= '9')|| single == '.'||single == '/'){
                return YES;
            }else{
                [self showError:@"只能输入数字或/或."];
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;
            }
        }
    }
   
    BOOL isHaveDian = YES;
    if ([textField.text rangeOfString:@"."].location == NSNotFound) {
        isHaveDian = NO;
    }
   
    //输入的字符是否是小数点
    if ([string length] > 0) {
        unichar single = [string characterAtIndex:0];//当前输入的字符
        if ((single >= '0' && single <= '9') || single == '.'|| single == '-') {//数据格式正确
            //判断长度
            if(!isHaveDian && [textField.text length] >= 10){
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;
            }
            //首字母不能为0和小数点
            if([textField.text length] == 0){
                if(single == '.') {
                    [self showError:@"第一个数字不能为小数点"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
                if (single == '0') {
                    if (self.shuzio){
                        return YES;
                    }
                    [self showError:@"第一个数字不能为0"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            //输入的字符是否是小数点
            if (single == '.') {
                if(!isHaveDian)//text中还没有小数点
                {
                    isHaveDian = YES;
                    return YES;
                }else{
                    [self showError:@"您已经输入过小数点了"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }else{
                if (isHaveDian) {//存在小数点
                    //判断小数点的位数
                    NSRange ran = [textField.text rangeOfString:@"."];
                    if (range.location - ran.location <= 2) {
                        return YES;
                    }else{
                        [self showError:@"最多输入两位小数"];
                        return NO;
                    }
                }else{
                    return YES;
                }
            }
        }else{//输入的数据格式不正确
            [self showError:@"输入的格式不正确"];
            [textField.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
    }
    return YES;
}

-(void)showError:(NSString *)msg{
    [LSUtility showToastWithMessage:msg inView:[UIApplication sharedApplication].keyWindow Andtime:1];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end
